import datetime
import sys

import FilePathMoudel


def getMergeFiles(paths: list[str], suffixNames: list[str]) -> list[str]:
	result: list[str] = []
	for path in paths:
		path = path.replace("'", "").replace('"', '')
		files = FilePathMoudel.getPathFilesNames(path)
		for file in files:
			try:
				for suffixName in suffixNames:
					if file.endswith(suffixName):
						result.append(file)
						break
			except:
				msg = FilePathMoudel.getSysExcInfoErrorMsg()
				sys.stderr.write(msg)
				FilePathMoudel.writeLogFile(msg, "getMergeFiles")
	result = sorted(result, key=len)
	return result


def checkStr(checkStr: str) -> bool:
	checkStr = FilePathMoudel.strRemoveSpaces(checkStr)
	if len(checkStr) == 0:
		return False
	return True


def getContent(readFilePath: str):
	content = FilePathMoudel.readFile(readFilePath, encoding=None)
	if len(content) == 0:
		return ""
	lines = content.split("\n")
	content = ""
	for line in lines:
		if checkStr(line):
			content = f"{content}\t{line}\n"
	content = f"文件路径\t{readFilePath}:\n{content}"
	return content


# 按装订区域中的绿色按钮以运行脚本。
if __name__ == '__main__':

	argvs = FilePathMoudel.initParamArgs()
	mergeContent = f"运行指令 : {sys.argv}\n\n"
	print(mergeContent)
	lines: list[str] = []
	suffixNames = argvs.get("e")
	paths = argvs.get("p")
	for file in getMergeFiles(paths=paths, suffixNames=suffixNames):
		content = getContent(file)
		print(f"读取 {FilePathMoudel.getPathAbsName(file)} 内容")
		mergeContent = f"{mergeContent}{content}"
	name = argvs.get("n")
	now = ""
	if name and len(name) > 0:
		now = name[0]
	else:
		now = datetime.datetime.now().strftime("%Y年%m月%d日%H时%M分")
	for writePath in argvs.get("o"):
		writePath = writePath.replace("'", "").replace('"', '')
		writePath = FilePathMoudel.getPathAbsName(writePath)
		writePath = f"{writePath}/{now}.txt"
		FilePathMoudel.writeFile(mergeContent, writePath)
		print(f"\n===========\n输出路径 : {writePath}\n===========")
	...
